package com.ymatou.iapi.productmanager.testcase;

import com.ymatou.iapi.productmanager.parameter.UpdateProductCommisionBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productmanager.service.UpdateProductCommisionCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.math.BigDecimal;

/**
*更改平台佣金
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UpdateProductCommision{
	private static UpdateProductCommisionBean updateproductcommisionBean;
	private static UpdateProductCommisionCall updateproductcommisionCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("globalSellerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更改平台佣金");
	}

	@Before
	public void caseUp() {
		updateproductcommisionBean=new UpdateProductCommisionBean();
		updateproductcommisionCall=new UpdateProductCommisionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(updateproductcommisionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCommision_001() {
		Logger.start(true,"更改平台佣金");
		try {
			AddSellerProductRequest addSellerProductRequest =  ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			ProductManagerCallServiceV2.AddSellerGlobalPspProduct(sellerId,productId,1);

			updateproductcommisionBean.setUserId(addSellerProductRequest.getSellerId());
			updateproductcommisionBean.setProductId(productId);
			updateproductcommisionBean.setSellerName(addSellerProductRequest.getSellerName());
			BigDecimal commission = new BigDecimal(100);

			updateproductcommisionBean.setCommission(commission);
			updateproductcommisionCall.setData(updateproductcommisionBean);
			updateproductcommisionCall.callService();
			Logger.verifyEquals("200",updateproductcommisionCall.getString("Code"),"验证Code");
			YmtProductsI ymtProductsI = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
//			Logger.verifyEquals(commission.intValue(),ymtProductsI.getCommission().intValue(),"验证commission");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCommision_002() {
		Logger.start(true,"更改平台佣金，佣金为负数");
		try {
			AddSellerProductRequest addSellerProductRequest =  ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			updateproductcommisionBean.setUserId(addSellerProductRequest.getSellerId());
			updateproductcommisionBean.setProductId(productId);
			updateproductcommisionBean.setSellerName(addSellerProductRequest.getSellerName());
			BigDecimal commission = new BigDecimal(-100);
			updateproductcommisionBean.setCommission(commission);
			updateproductcommisionCall.setData(updateproductcommisionBean);
			updateproductcommisionCall.callService();
			Logger.verifyEquals("201",updateproductcommisionCall.getString("Code"),"验证Code");
			YmtProductsI ymtProductsI = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
			Logger.verifyEquals(0,ymtProductsI.getCommission().intValue(),"验证commission");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCommision_003() {
		Logger.start(true,"更改平台佣金，商品不存在");
		try {
			AddSellerProductRequest addSellerProductRequest =  ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			updateproductcommisionBean.setUserId(addSellerProductRequest.getSellerId());
			updateproductcommisionBean.setProductId("hwqohdio");
			updateproductcommisionBean.setSellerName(addSellerProductRequest.getSellerName());
			BigDecimal commission = new BigDecimal(100);
			updateproductcommisionBean.setCommission(commission);
			updateproductcommisionCall.setData(updateproductcommisionBean);
			updateproductcommisionCall.callService();
			Logger.verifyEquals("201",updateproductcommisionCall.getString("Code"),"验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UpdateProductCommision_004() {
		Logger.start(true,"更改平台佣金，非全球优选商品");
		try {
			AddSellerProductRequest addSellerProductRequest =  ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(3383);
			String productId = ProductManagerCallServiceV2.addNewProduct(addSellerProductRequest).get("Data").getAsJsonObject().get("ProductId").getAsString();
			updateproductcommisionBean.setUserId(addSellerProductRequest.getSellerId());
			updateproductcommisionBean.setProductId(productId);
			updateproductcommisionBean.setSellerName(addSellerProductRequest.getSellerName());
			BigDecimal commission = new BigDecimal(100);
			updateproductcommisionBean.setCommission(commission);
			updateproductcommisionCall.setData(updateproductcommisionBean);
			updateproductcommisionCall.callService();
			Logger.verifyEquals("201",updateproductcommisionCall.getString("Code"),"验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}